import modelExtend from 'dva-model-extend'
import {
  query,
  deny,
  accept,
  getChangeDetail,
  store,
  update,
  submit,
  cancel
} from 'services/orderChangeApply'
import {show, findByContractNum} from 'services/contractOrders'

import {lwjPageModel} from 'utils/model'
import {routerRedux} from 'dva/router'
import {getRegionCodes} from 'utils/utils';

export default modelExtend(lwjPageModel, {
  namespace: 'orderChangeApply',
  state: {
    currentItem: {},
    createModalVisible: false,
    auditModalVisible: false,
    viewModalVisible: false,
    contractModalVisible: false,
    editVisible: false,
    changeType: 'INFORMATION',
    selectedTags: [],
    record: {},
    isSub: false
  },
  subscriptions: {
    setup({dispatch, history}) {
      history.listen(({pathname}) => {
        /**
         * 列表页的监听
         */
        if (pathname === '/order/orderChangeApply' || pathname === '/order/modify') {
          dispatch({type: 'query', payload: {}})
        }
      })
    }
  },
  effects: {
    * query({
      payload
    }, {call, put, select}) {
      yield put({
        type: 'asyncData',
        payload: {
          ...payload
        }
      })
      const pageOptions = yield select(({orderChangeApply}) => orderChangeApply.pageOptions)
      const queryParam = yield select(({orderChangeApply}) => orderChangeApply.queryParam)

      const queryPayload = {
        ...pageOptions,
        queryParam: queryParam
      }

      const data = yield call(query, queryPayload)

      yield put({
        type: 'querySuccess',
        payload: {
          ...data
        }
      })
    },

    * getDetail({
      payload
    }, {call, put}) {
      const {id} = payload
      let res = yield call(getChangeDetail, id)

      if (res.success) {
        yield put({type: 'showSuccess', payload: res.result})
      }
    },

    * store({
      payload
    }, {call, put, select}) {
      const currentItem = yield select(({contractOrders}) => contractOrders.currentItem)
      payload.contractNum = currentItem.contractNum
      payload.contractOrderId = currentItem.id

      if (typeof payload.consigneeRegion !== 'undefined' && Array.isArray(payload.consigneeRegion)) {
        payload.province = payload.consigneeRegion[0]
        payload.city = payload.consigneeRegion[1]
        payload.district = payload.consigneeRegion[2]
        delete payload.consigneeRegion
      }
      const res = yield call(store, payload)

      if (res.success) {
        yield put({type: 'hideModal', payload: {}})
      }
    },

    * accept({
      payload
    }, {call, put, select}) {
      const {id, version, values} = payload
      const res = yield call(accept, id, version, values)
      if (res.success) {
        yield put({type: 'hideModal', payload: {}})
      }
    },

    * update({
      payload
    }, {call, put, select}) {
      const res = yield call(update, payload)
      if (res.success) {
        yield put({type: 'hideModal', payload: {}})
        yield put(routerRedux.push(`/order/orderChangeApply`));
      }
    },

    * deny({
      payload
    }, {call, put, select}) {
      const {id, values} = payload
      const res = yield call(deny, id, values)
      if (res.success) {
        yield put({type: 'hideModal', payload: {}})
      }
    },
    * cancel({
      payload
    }, {call, put, select}) {
      const {id} = payload
      const res = yield call(cancel, id)
      if (res.success) {
        yield put(routerRedux.push(`/order/orderChangeApply`));
      }
    },

    * subDraft({
      payload
    }, {call, put, select}) {
      if (payload.subStatus) {
        const {id, version, values} = payload
        const res = yield call(submit, id, version, values)
        if (res.success) {
          yield put({type: 'hideModal', payload: {}})
          yield put(routerRedux.push("/order/orderChangeApply"));
        }
      } else {
        yield put({type: 'hideModal', payload: {}})
      }
      yield put({
        type: 'isSub',
        payload: {
          isSub: false
        }
      })

    },

    /**
     * 根据状态标签查询
     * @param {*} param0
     * @param {*} param1
     */
    * queryBySelectedTags({
      payload
    }, {select, call, put}) {
      yield put({
        type: 'changeSelectedTags',
        payload: {
          selectedTags: payload.selectedTags
        }
      })
      yield put({
        type: 'query',
        payload: {
          'statusList': payload.selectedTags
        }
      })
    },

    * onShow({
      payload
    }, {call, put, select}) {

      yield put({type: 'clearCurrentItemState', payload: {}})
      const contractOrder = yield call(findByContractNum, payload)
      const record = yield select(({orderChangeApply}) => orderChangeApply.record)
      // const res = yield call(show, record.id) let data = res.result 拼装地址信息
      if (contractOrder.code === 0) {
        const res = yield call(getChangeDetail, record.id)
        let data = res.result

        // 拼装地址信息
        data.consigneeRegion = getRegionCodes(data.province, data.city, data.district);
        if (res.success) {
          yield put({type: 'setCurrentItem', payload: data})
        }
      }
    }
  },
  reducers: {
    setChangeType(state, {payload}) {
      state.changeType = payload.type

      return {
        ...state
      }
    },

    setRecord(state, {payload}) {
      state.record = payload.record
      return {
        ...state
      }
    },
    isSub(state, {payload}) {
      state.isSub = payload.isSub
      return {
        ...state
      }
    },

    changeSelectedTags(state, {payload}) {
      return {
        ...state,
        selectedTags: payload.selectedTags
      }
    },

    openAuditModal(state, {payload}) {
      state.auditModalVisible = true

      return {
        ...state
      }
    },
    openViewModal(state, {payload}) {
      state.viewModalVisible = true

      return {
        ...state
      }
    },
    onChangeModal(state, {payload}) {
      state.createModalVisible = true

      return {
        ...state
      }
    },
    setCurrentItem(state, {payload}) {
      state.currentItem = payload
      return {
        ...state
      }
    },
    clearCurrentItemState(state, {payload}) {
      state.currentItem = {}
      return {
        ...state
      }
    },

    openContractModal(state, {payload}) {
      state.contractModalVisible = true

      return {
        ...state
      }
    },

    showEditModal(state, {payload}) {
      return {
        ...state,
        editVisible: true
      }
    },

    hideEditModal(state, {payload}) {
      return {
        ...state,
        editVisible: false
      }
    },

    hideContractModal(state, {payload}) {
      state.contractModalVisible = false

      return {
        ...state
      }
    },

    /**
     * 隐藏所有modal
     * @param state
     * @param payload
     * @return {{}}
     */
    hideModal(state, {payload}) {
      state.createModalVisible = false
      state.auditModalVisible = false
      state.viewModalVisible = false
      state.editVisible = false
      return {
        ...state
      }
    }
  }
})
